<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/c" prefix="c" %>
<%@taglib prefix="display" uri="http://displaytag.sf.net/el"%>
<%@ page import="java.text.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.lang.*"%>
<%@ page import="com.solution.webconstant.PermissionConstants" %>
<script type="text/javascript" src="../js/prototype.js"></script>
<%
	HashMap permissionMap = ((HashMap)(session.getAttribute("userPermissionsInfo")));
%>
<script language="JavaScript">
var userArray = new Array();
var userRowID= 1;
function addRow(obj){
	//var div = new Element('div', { 'id': 'hierarchyRow'+hierarchyRowID,'class':'empty'});
	var content = $('userRow').innerHTML;
	
	content = content.replace(/rowId/,""+userRowID);
	if($('first_name_temp').value.strip()=="")
	{
		alert("Please enter First Name");
		$('first_name_temp').focus();
		return;
	}
	if($('last_name_temp').value.strip()=="")
	{
		alert("Please enter Last Name");
		$('last_name_temp').focus();
		return;
	}
	if($('user_name_temp').value.strip()=="")
	{
		alert("Please enter User Name");
		$('user_name_temp').focus();
		return;
	}
	if(examNameExist){
		alert("Please enter Uniue User Name");
		$('user_name_temp').focus();
		return;
	}
	if($('password_temp').value.strip()=="")
	{
		alert("Please enter password_temp");
		$('password_temp').focus();
		return;
	}
	
	content = content.replace(/firstName/,$('first_name_temp').value);
	content = content.replace(/lastName/,$('last_name_temp').value);
	content = content.replace(/userName/,$('user_name_temp').value);
	content = content.replace(/password/,"********");

	
	content = content.replace(/firstNameValue/,$('first_name_temp').value);
	content = content.replace(/lastNameValue/,$('last_name_temp').value);
	content = content.replace(/userNameValue/,$('user_name_temp').value);
	content = content.replace(/passwordValue/,$('password_temp').value);
	content = content.replace(/userRowData/,"1");
	
	content = content.replace(/user_name_array_id/,"user_name_array_id"+userRowID);
	if(userArray[$('user_name_temp').value]!= "added") {
		userArray[$('user_name_temp').value] = "added";
	}else{
		alert("User Name Already added");
		return;
	}
	
	
	content = content.replace(/<TBODY>/i,"");
	content = content.replace(/<\/TBODY>/i,"");
	$('userHeaderRow').style.display = "";
	userRowID++;
	$('temp').insert({after:content});	
}
function removeRow(obj){
	userArray[$('user_name_array_id'+obj.id).value]= "";
	$(obj.id).up(1).remove();
	userRowID--;
}
var examNameExist = false;
function isNameExist(){
	var userName = $('user_name_temp').value;
	new Ajax.Request('/isUserNameExist.do?user_name='+userName,   
			{     method:'get',     
				  onSuccess: function(transport)
					{       
						var response = transport.responseText || "no response text";
						if(response=="TRUE"){
							examNameExist=  true;
							$('userNameId').innerHTML = "User name exist!"
							$('userNameId').style.display = "";
						}else{
							examNameExist = false;
							$('userNameId').innerHTML="";
							$('userNameId').style.display = "none";
						}
					},     
				  onFailure: function()
				    { 
						alert('Something went wrong...') 
					}   
			}
		); 
}
function fnSubmit()
{
	var userTypeArray = document.getElementsByName("user_type");
	if(!userTypeArray[0].checked && !userTypeArray[1].checked){
		alert("Please select User type.");		
		return;
	}
	if(!userTypeArray[0].checked){	
		if($('exam_type').value==""){
			alert("Please select exam type.");
			$('exam_type').focus();
			return;
		}
	}
	
	if(userRowID==1){
		alert("Please Add User using 'Add User' button.");
		return;
	}
	$('userRowData').innerHTML = '';	
	document.userForm.action ='/addUser.do?';
	document.userForm.submit();
}
function fnEditViewUserDetails(exam_id_value, screen_type_value){
	document.userForm.screen_type.value=screen_type_value;
	document.userForm.user_id.value=exam_id_value;
	document.userForm.action='/editViewUser.do?';
	document.userForm.submit();
}
function clearForm(formObj){
	var elements = formObj.elements;
	for(i=0; i<elements.length; i++){
		if(elements[i].type == "text")
			elements[i].value = "";
		if(elements[i].type == "select-one" || elements[i].type == "select-multiple")
			elements[i].selectedIndex = 0;
	}
}
function enableDisableExamType(userType){
	if(userType=='1'){
		$('exam_type').disabled = true;
	}else{
		$('exam_type').disabled = false;
	}

}

</script>
<script language="JavaScript" src="js/Calendar.js" type="text/JavaScript"></script>
<html:form action="/user">
	
	<html:hidden property="user_id" />
	<table width="550" align="center" border="0" cellspacing="0" cellpadding="0">
		<tr>
			<td>&nbsp;</td>
		</tr>
		<tr>
 			<td class="heading1">Add Users</td>
		</tr>
		<tr>
			<td valign="top">&nbsp;</td>
		</tr>
		<tr>
			<td valign="top"><jsp:include page="showErrorMessage.jsp"/></td>
		</tr>
	  	<tr>
			<td>
				<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
					<tr>
						<td align="left" valign="top" class="inputtext">First Name<font class="asterix">*</font></td>
						<td valign="middle" ><input name="first_name_temp" id="first_name_temp" class="form1"/></td>
					</tr>
					<tr>
						<td align="left" valign="top" class="inputtext">Last Name<font class="asterix">*</font></td>
						<td valign="middle"><input name="last_name_temp" id="last_name_temp" class="form1"/></td>
					</tr>
					<tr>
						<td align="left" valign="top" class="inputtext">User Name<font class="asterix">*</font></td>
						<td valign="middle" ><input name="user_name_temp" id="user_name_temp" class="form1" onblur="javascript:isNameExist();"/><div id="userNameId" class="errorMsg" style="display:none;"  ></div></td>
					</tr>
					<tr>
						<td align="left" valign="top" class="inputtext">Password<font class="asterix">*</font></td>
						<td valign="middle" ><input name="password_temp" type="password" id="password_temp" class="form1" /></td>
					</tr>	
					<tr>
						<td height="25" colspan="4" valign="top"  >&nbsp;</td>
					</tr>
					<tr >
						<td height="25" colspan="4" align="center" valign="top">
							<input type="button" class="imageButton" id="addUserButton" value="Add User" onClick="javascript:addRow($('temp'))">
						</td>
					</tr>
					<tr>
						<td height="25" colspan="4" valign="top"  >
							<table width="550" border="0" cellspacing="0" cellpadding="0" class="tbletextTemp" id="userHeaderRow" style="display:none;">
								
								<tr id="temp">
									<td align="left" ><b>First Name</b></td><td align="left"><b>Last Name</b></td><td align="left" ><b>User Name</b></td>
									<td align="left" ><b>Password</b></td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td height="25" colspan="4" valign="top"  >&nbsp;</td>
					</tr>
					<tr>
						<td colspan="3" class="heading2">User Type</td>
					</tr>
					<tr>
						<td height="10" colspan="4" valign="top"  >&nbsp;</td>
					</tr>
					<tr>
						<td><html:radio name="userForm" property="user_type" styleId="user_type_examiner" value="1" onclick="javascript:enableDisableExamType('1');" /></td>
						<td class="inputtext">Examiner</td>
					</tr>
					<tr>
						<td><html:radio name="userForm" property="user_type" styleId="user_type_examinee" value="2" onclick="javascript:enableDisableExamType('2');"/>
						</td>
						<td class="inputtext">Examinee</td>
					</tr>
					<tr>
						<td align="left" valign="top" class="inputtext">Exam Type</td>
						<td valign="middle">
							<html:select property="exam_type" styleId="exam_type" styleClass="form3">
							<html:option value="">--Please Select--</html:option>
							<logic:present name="userForm" property="exam_type_list">
							<logic:iterate name="userForm" property="exam_type_list" id="examType" >
							<bean:define id="dID" name="examType" property="id" />
							<html:option  value="<%=dID.toString()%>"><bean:write name="examType" property="name"/></html:option>		
							</logic:iterate>
							</logic:present>
							</html:select>
						</td>	
					</tr>
					<tr>
						<td colspan="4" valign="top"  ></td>
					</tr>
					<tr>
						<td height="25" colspan="4" align="center" valign="top" >&nbsp;</td>
					</tr>
					<tr>
						<td colspan="4" valign="top"  bgcolor="#CCCCCC"><img src="images/zero.gif" width="1" height="1" /></td>
					</tr>
					<tr>
						<td height="25" colspan="4" align="center" valign="top" >&nbsp;</td>
					</tr>
					<tr>
       	 				<td height="25" colspan="4" align="center" valign="top">
       	 					<input type="button" class="imageButton" value="Submit" onClick="JavaScript:fnSubmit()">
       	 					<input type="button" class="imageButton" value="Reset" onClick="JavaScript:clearForm(this.form)">
       	 				</td>
      				</tr>
      				<tr>
						<td height="25" colspan="4" align="center" valign="top" >&nbsp;</td>
					</tr>
				</table>
          	</td>
		</tr>
      </table>
	  <table width="150" border="0" cellspacing="0" cellpadding="0" id="userRow" style="display:none;">
		<tr>
			<td  align="left"><img id="rowId" valign="bottom" src="images/minus.gif" onClick="javascript:removeRow(this);"/> firstName</td>
			<td  align="left">lastName</td>
			<td  align="left" >userName</td>
			<td  align="left" >password<div  id="userRowData">
			<input type="hidden" name="first_name_array" value="firstNameValue">
			<input type="hidden" name="last_name_array" value="lastNameValue">
			<input type="hidden" name="user_name_array" id="user_name_array_id" value="userNameValue">
			<input type="hidden" name="password_array" id="password_array_id" value="passwordValue">
			</div>
			</td>
		</tr>
	</table>
</html:form>